import { APIAuthorizationAdd, APIAuthorizationList, APIUserSetConfig } from "@/api/apiSetting"
import { getRobotList } from "@/api/cloud"
import { reactive } from "vue"

interface PaginationData {
  currentPage: number
  pageSize: number
  count: number
}

interface AuthorizationList {
  id: number
  user_id: number
  name: string
  authorization: string
  exp: number
  status: string
  createtime: number
  admin_nickname: string
  admin_avatar: string
}

interface State {
  paginationData: PaginationData
  authorizationList: AuthorizationList[]
  authorizationListLoading: boolean
  projectCount: number
  memberCount: number
  memberList: any
  machineCount: number
}

/** 默认的分页参数 */
const defaultPaginationData: PaginationData = {
  currentPage: 1,
  pageSize: 20,
  count: 0
}

const state: { [key: string]: any } = reactive<State>({
  paginationData: { ...defaultPaginationData },
  authorizationList: [],
  authorizationListLoading: false,
  projectCount: 0,
  memberList: [
    //mock
    {
      text: "对话",
      img: "icon-a-mianxingguanliyuan"
    },
    {
      text: "绘图",
      img: "icon-a-mianxingAIshengtu"
    },
    {
      text: "语音合成",
      img: "icon-a-mianxingyuyin"
    },
    {
      text: "视频生成",
      img: "icon-AIshipin"
    }
  ],
  memberCount: 4,
  machineCount: 0
})

export default function useSpeechModel() {
  // 获取我的项目通讯token
  const getAuthorizationList = async (nextPage?: string) => {
    if (!nextPage) {
      state.paginationData.currentPage = 1
    }
    state.authorizationListLoading = true
    let { data, code } = await APIAuthorizationList({ page: state.paginationData.currentPage })
    if (code === 200) {
      state.authorizationList = nextPage ? state.authorizationList.concat(data.list) : data.list
    }

    state.paginationData.count = data.count
    state.projectCount = data.count
    state.authorizationListLoading = false
  }

  // 生成项目token
  const getAPIAuthorizationAdd = async (params: any) => {
    state.authorizationListLoading = true
    let { data, code } = await APIAuthorizationAdd(params)
    if (code === 200) {
      getAuthorizationList()
    }
  }

  // 小机器总数
  const getRobotListConut = async () => {
    const { data, code } = await getRobotList({ page: 1, label_id: 0 })
    if (code === 200) {
      state.machineCount = data.count
    }
  }

  // 设置项（url回调）
  const getUserSetConfig = async (params: any) => {
    const { data, code } = await APIUserSetConfig(params)
    if (code === 200) {
      state.machineCount = data.count
    }
  }

  // 手动翻页
  const authorizationCurrentChange = (value: number) => {
    state.paginationData.currentPage = value
    getAuthorizationList("nextPage")
  }

  // myProject滚动下一页
  const myProjectNextPage = () => {
    if (state.paginationData.currentPage < state.paginationData.count / state.paginationData.pageSize) {
      state.paginationData.currentPage += 1
      console.log("滚动下一页")

      getAuthorizationList("nextPage")
    }
    console.log(state.paginationData.currentPage)
  }
  // ...
  return {
    myProjectNextPage,
    authorizationCurrentChange,
    getUserSetConfig,
    getRobotListConut,
    getAuthorizationList,
    state,
    getAPIAuthorizationAdd
  }
}
